package com.system.leave.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.AttendanceCherkDepartment.entity.Attendance;
import com.system.AttendanceCherkDepartment.service.IAttendanceService;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.common.util.SessionUtil;
import com.system.common.util.WorkflowVariable;
import com.system.employee.entity.Employee;
import com.system.employee.service.IEmployeeService;
import com.system.leave.dto.LeaveDTO;
import com.system.leave.entity.Leave;
import com.system.leave.service.ILeaveService;


@Controller
@RequestMapping(value="/leave")
public class LeaveController 
{
	@Autowired
	private ILeaveService leaveService;
	
	@Autowired
	private IAttendanceService attendanceService;
	@Autowired
	private IEmployeeService employeeService;
	
	
	@RequestMapping(value = "/saveOrUpdate")
    public @ResponseBody ExtAjaxResponse saveOrUpdate(Leave leave) {
    	try {
//    		Attendance result = new Attendance();
//    		//找到所有employee
//    		List<Employee> emplist=employeeService.findAll();
//    		//找到该员工attendance记录
//    		attendanceService.save(leave.getLeaveType(),leave.getUserId());
//    		for(Employee emp:emplist)
//    		{
//    			
//				if(emp.getUser().getUid()==leave.getUserId())
//				{
//					result=attendanceService.findOnebyEmp(emp.getEmpId());
//					System.out.println(result);
//				    break;
//				}
//    		}
//    		  if(null!=result && null!=leave) {
//			    		if((result.getTotalLeave()>=3) && (leave.getLeaveType()=="A"))
//			    		{
//			    			
//			    			return new ExtAjaxResponse(true,"请假次数大于3!将会扣除相应工资！");
//			    		}
//			    		
//			    		else if((result.getTotalVacation()>=4) && leave.getLeaveType()=="B")
//			    		{
//			    			return new ExtAjaxResponse(false,"休假次数耗尽(>=4)!");
//			    		}
//			    			
//			
			    		leaveService.save(leave);
//			    		attendanceService.save(leave.getLeaveType(),leave.getUserId());
//    		  }
			    		return new ExtAjaxResponse(true,"添加成功!");
    		       
	    } catch (Exception e) {
	    	
	    	
	    	
	    	e.printStackTrace();
	        return new ExtAjaxResponse(false,"操作失败!");
	    }
    }
	
	@RequestMapping(value = "/delete")
    public @ResponseBody ExtAjaxResponse delete(Long id) {
    	try {
    		leaveService.delete(id);
    		return new ExtAjaxResponse(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new ExtAjaxResponse(false,"操作失败!");
	    }
    }
	
	
	@RequestMapping(value = "/leave-list")
    public @ResponseBody Page<Leave> findLeaveByUserId(HttpSession session,ExtPageable pageable) {
		Page<Leave> page = new PageImpl<Leave>(new ArrayList<Leave>(), pageable.getPageable(), 0);
		try {
    		//String userId = SessionUtil.getUserName(session);
    		String userId= (String) session.getAttribute("userId");
    		System.out.println("get-userId:"+userId);
    		page = leaveService.findLeave(userId, pageable.getPageable());
	    } catch (Exception e) {
	       e.printStackTrace();
	    }
		return page;
    }
	/*-------------------------------------流程引擎web层------------------------------------------*/
	
	/**
	 * 启动流程
	 * @param leaveId	请假信息Id
	 * @param session	通过会话获取登录用户(请假人)
	 * @return
	 */
	@RequestMapping(value = "/start")
    public @ResponseBody ExtAjaxResponse start(@RequestParam(name="id") Long leaveId,HttpSession session) {
    	try {
    		Map<String, Object> variables = new HashMap<String, Object>();
    		variables.put("deptLeader", "hr");//andy
    		variables.put("applyUserId", (String) session.getAttribute("userId"));
    		leaveService.startWorkflow(leaveId, variables);
    		
    		return new ExtAjaxResponse(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new ExtAjaxResponse(false,"操作失败!");
	    }
    }
	
	/**
	 * 查询待处理流程任务
	 * @param pageable	分页对象
	 * @param session	通过会话获取登录用户(请假人)
	 * @return
	 */
	@RequestMapping(value = "/findTasks")
    public @ResponseBody Page<LeaveDTO> findTodoTasks(HttpSession session,ExtPageable pageable) {
		Page<LeaveDTO> page = new PageImpl<LeaveDTO>(new ArrayList<LeaveDTO>(), pageable.getPageable(), 0);
    	try {
    		page = leaveService.findTodoTasks((String) session.getAttribute("userId"), pageable.getPageable());
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
    	
    	return page;
    }
	
	/**
     * 签收任务
     */
    @RequestMapping(value = "claim/{id}")
    public @ResponseBody ExtAjaxResponse claim(@PathVariable("id") String taskId, HttpSession session) {
    	try{
	    	leaveService.claim(taskId, (String) session.getAttribute("userId"));
	    	return new ExtAjaxResponse(true,"任务签收成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new ExtAjaxResponse(false,"任务签收失败!");
	    }
    }
    
    /**
     * 完成任务
     * @param id
     * @return
     */
    @RequestMapping(value = "complete/{id}")
    public @ResponseBody ExtAjaxResponse complete(@PathVariable("id") String taskId, WorkflowVariable var) {
    	try{
    		Map<String, Object> variables = var.getVariableMap();
	    	leaveService.complete(taskId, variables);
	    	return new ExtAjaxResponse(true,"任务签收成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new ExtAjaxResponse(false,"任务签收失败!");
	    }
    }
}
